<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar bg-white" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('manage.consumer.personauthreview')}}
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar+5 + 'px 10px 0px 10px'}]"></view>
		<view class="data-box">
			<view class="cu-bar bg-white search">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="$t('global.plsuserid')" v-model="searchText"  @input="searchTag"/>
					<view class="search-close" v-if="searchText!=''" @tap="searchClose()">
						<text class="cuIcon-close"></text>
					</view>
				</view>
			</view>
			<view class="search-type grid col-2">
				<view class="search-type-box" @tap="toType(0)" :class="type==0?'active':''">
					<text>{{$t('global.approval')}}</text>
				</view>
				<view class="search-type-box" @tap="toType(1)" :class="type==1?'active':''">
					<text>{{$t('global.passed')}}</text>
				</view>
			</view>
			<view class="identify-list">
				<view class="no-data" v-if="identifyList.length==0">
					<text class="cuIcon-text"></text>{{$t('common.nodata')}}
				</view>
				<view class="identify-item-box" v-for="(item,index) in identifyList" :key="index">
					<view class="identify-box-concent">
						<view class="identify-box-value">
							{{$t('manage.consumer.username')}}:<text>{{item.name}}</text>
						</view>
						<view class="identify-box-value">
							{{$t('manage.consumer.idcard')}}:<text>{{item.idCard}}</text>
						</view>
					</view>
					<view class="identify-box-user">
						<text class="text-blue">
							<text class="cuIcon-people">{{item.userJson.name}}</text>
						</text>
						<text class="identify-box-btn">
							<block v-if="item.identifyStatus==0">
								<text class="text-green margin-right-sm" @tap="autoReview(item.uid)">{{$t('manage.consumer.autoreview')}}</text>
								<text class="text-blue margin-right-sm" @tap="systemReview(item.uid)">{{$t('manage.consumer.manualreview')}}</text>
								<text class="text-red" @tap="toDelete(item.uid,0)">{{$t('global.nopass')}}</text>
							</block>
							<block v-else>
								<text class="text-red" @tap="toDelete(item.uid,1)">{{$t('manage.consumer.revokeauth')}}</text>
							</block>
						</text>
					</view>
				</view>
				<view class="load-more" @tap="loadMore" v-if="identifyList.length>0">
					<text>{{moreText}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				moreText:$t('common.loadmoretxt'),
				userInfo:"",
				token:'',
				searchText:'',
				type:0,
				identifyList:[],
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getIdentifyList(false);
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}
			that.getCacheInfo();
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(localStorage.getItem('token')){
				that.token=localStorage.getItem('token');
				that.getIdentifyList(false);
			}
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			getCacheInfo(){
				var that = this;
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					that.uid=userInfo.uid;
					that.userInfo = userInfo;
				}
			},
			toType(i){
				var that = this;
				that.type=i;
				that.page=1;
				that.moreText = that.$t('common.loadmoretxt');
				that.isLoad=0;
				that.identifyList = [];
				that.getIdentifyList(false);
			},
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getIdentifyList(true);
			},
			searchTag(){
				var that = this;
				var searchText = that.searchText;
				that.page=1;
				that.getIdentifyList();
			},
			getIdentifyList(isPage){
				var that = this;
				var data = {
					"identifyStatus":that.type,
				}
				if(that.searchText!=""){
					data.uid = that.searchText;
				}
				var page = that.page;
				if(isPage){
					page++;
				}
				that.$Net.request({
					url: that.$API.consumerList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":8,
						"page":page,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.stopPullDownRefresh()
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								if(isPage){
									that.page++;
									that.identifyList = that.identifyList.concat(list);
								}else{
									that.identifyList = list;
								}
							}else{
								that.moreText = that.$t('manage.consumer.nomorecontent');
								if(!isPage){
									that.identifyList = list;
								}
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.stopPullDownRefresh()
						that.moreText = that.$t('common.loadmoretxt');
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			systemReview(uid){
				var that = this;
				var token = "";
				
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.consumer.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.systemIdentifyConsumer(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
										uni.request({
											url:that.$API.SPgetconsumer(),
											method:'GET',
											data:{
												uid:uid,
											},
											dataType:"json",
											success(res) {
											},
											fail() {
												setTimeout(function () {
													uni.hideLoading();
												}, 1000);
												uni.showToast({
													title: that.$t('common.networkfail'),
													icon: 'none'
												})
											}
										})
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getIdentifyList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            
				        }
				    }
				});
			},
			autoReview(uid){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.consumer.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.identifyConsumer(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
										uni.request({
											url:that.$API.SPgetconsumer(),
											method:'GET',
											data:{
												uid:uid,
											},
											dataType:"json",
											success(res) {
											},
											fail() {
												setTimeout(function () {
													uni.hideLoading();
												}, 1000);
												uni.showToast({
													title: that.$t('common.networkfail'),
													icon: 'none'
												})
											}
										})
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getIdentifyList(false);
				            		}
				            		
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            
				        }
				    }
				});
			},
			toDelete(uid,type){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"uid":uid,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.consumer.sureperformopt'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.removeConsumer(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
										if(type==1){
											uni.request({
												url:that.$API.SPgetunconsumer(),
												method:'GET',
												data:{
													uid:uid,
												},
												dataType:"json",
												success(res) {
												},
												fail() {
													setTimeout(function () {
														uni.hideLoading();
													}, 1000);
													uni.showToast({
														title: that.$t('common.networkfail'),
														icon: 'none'
													})
												}
											})
										}
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getIdentifyList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            
				        }
				    }
				});
			},
		}
	}
</script>